package StudyCode.summary.myfile;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertByteArrayToHex {

	private static final char[] HEX_SOURCE = {'0', '1', '2', '3', '4',
			'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

	public static void main(String[] argv) {
		byte[] bytes = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		System.out.println("字节数组" + Arrays.toString(bytes) + "转为16进制格式:");
		System.out.println(toHex(bytes));
	}

	private static String toHex(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		sb.append("0x");
		for (byte b : bytes) {
			int v = b & 0xFF;
			sb.append(HEX_SOURCE[v >>> 4]).append(HEX_SOURCE[v & 0x0F]);
		}
		return sb.toString();
	}
}
